// pages/departDr/departDr.js
const app = getApp();
const request = require('../../utils/request.js');
const util = require('../../utils/util.js');
const ApiRootUrl = app.globalData.REQUEST_IP_HEAD;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    TabCur: 0,
    MainCur: 0,
    VerticalNavTop: 0,
    entList: [],
    load: true,
    department: [], //科室信息
    doctor: [], //医生信息
    departName:"", //科室名
    locate:"",  //科室位置
    detail:"",  //科室详情

    defaultDrAvator: ApiRootUrl + "/res/img/avatar/医生头像.png", 
    backImg: ApiRootUrl + "/res/img/miniApp/医疗主页背景.png"
  },
  /**
   * 跳转到医师详情页面
   */
  doctorDetail(e){
    wx.navigateTo({
      url: '/pages/doctorDetail/doctorDetail?d_id='+e.currentTarget.dataset.d_id,
    })
  },
  /**
   * 请求所有医生信息
   */
  getDoctor(){
    var that = this;
    request.get('/doctor/getAllValidDr', {}).then((res) => {
      if (res.code == 200) {
        that.setData({
          doctor: res.data
        })
      } else {
        util.showToast(res.msg, 'none');
      }
    }).catch((errMsg) => {
      util.showToast(errMsg, 'error');
    })
  },
  /**
   * 请求所有科室信息
   */
  getDepartment(){
    var that = this;
    request.get('/ent/getAllValidDrEnt', {}).then((res) => {
      if (res.code == 200) {
        //将获取到的科室列表赋给列表
        let list = res.data;
        that.setData({
          entList: list,
          listCur: list[0],
          departName: list[0].title,
          locate: list[0].location,
          detail: list[0].intro
        })
        that.getDoctor();
      } else {
        util.showToast(res.msg, 'none');
      }
    }).catch((errMsg) => {
      util.showToast(errMsg, 'error');
    })
  },
  /**
   * 跳转到聊天页面
   */
  chat(e){
    const that = this;
    if(wx.getStorageSync('userInfo').id!=null && wx.getStorageSync('userInfo').id  == e.currentTarget.dataset.d_id.uid){
      wx.showToast({
        title: '不能咨询自己！',
        icon: 'error'
      })
      return;
    }
    var doctor = e.currentTarget.dataset.d_id;
    var depart = e.currentTarget.dataset.depart_id;
    wx.navigateTo({
      url: '/pages/chat/chat?youId='+doctor.uid+'&youName='+doctor.name+'&youUnit='+depart.title+'&youAvatar='+that.data.defaultDrAvator,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getDepartment(); //获取科室医生数据
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getDepartment(); //获取科室医生数据
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  tabSelect(e) {
    this.setData({
      TabCur: e.currentTarget.dataset.index,
      MainCur: e.currentTarget.dataset.index,
      VerticalNavTop: e.currentTarget.dataset.index * 50,
      departName: this.data.entList[e.currentTarget.dataset.index].title,
      locate: this.data.entList[e.currentTarget.dataset.index].location,
      detail: this.data.entList[e.currentTarget.dataset.index].intro
    })
  },
  VerticalMain(e) {
    let that = this;
    let list = this.data.entList;
    let tabHeight = 0;
    if (this.data.load) {
      for (let i = 0; i < list.length; i++) {
        let view = wx.createSelectorQuery().select("#main-" + i);
        view.fields({
          size: true
        }, data => {
          list[i].top = tabHeight;
          tabHeight = tabHeight + data.height;
          list[i].bottom = tabHeight;     
        }).exec();
      }
      that.setData({
        load: false,
        entList: list
      })
    }
    let scrollTop = e.detail.scrollTop + 20;
    for (let i = 0; i < list.length; i++) {
      if (scrollTop > list[i].top && scrollTop < list[i].bottom) {
        that.setData({
          VerticalNavTop: i * 50,
          TabCur: i ,
          departName: this.data.entList[i].title,
          locate: this.data.entList[i].location,
          detail: this.data.entList[i].intro
        })
        return false
      }
    }
  }
})